package com.gok.seasonmovie.controller;

import com.gok.seasonmovie.entity.*;
import com.gok.seasonmovie.service.BookingService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/bookings")
public class BookingController {

    @Resource
    private BookingService bookingService;

//    @PostMapping("/byCid")
//    public Map<String, Object> getBookingsByCid(@RequestParam String cid) {
//        try{
//            Map<String, Object> response = new HashMap<>();
//            response.put("code", 200);
//            response.put("msg", "ok");
//            List<Booking> bookings = bookingService.findByCid(cid);
//            response.put("data", bookings);
//            return response;
//        }catch (RuntimeException e) {
//            Map<String, Object> errorResponse = new HashMap<>();
//            errorResponse.put("code", 600);
//            errorResponse.put("msg", "Database error: " + e.getMessage());
//            return errorResponse;
//        }
//    }

    @PostMapping("/getMovieById")
    public Map<String, Object> getMovieById(@RequestParam String id) {
        try{
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("msg", "ok");
            List<Movie> movies = bookingService.getMovieById(id);
            response.put("data", movies);
            return response;
        }catch (RuntimeException e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 600);
            errorResponse.put("msg", "Database error: " + e.getMessage());
            return errorResponse;
        }
    }

    @PostMapping("/getBookingByCidAndMid")
    public Map<String, Object> getBookingByCidAndMid(@RequestParam("cid") String cid,@RequestParam("mid") String mid) {
        try{
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("msg", "ok");
            List<Booking> bookings = bookingService.getBookingByCidAndMid(cid,mid);
            response.put("data", bookings);
            return response;
        }catch (RuntimeException e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 600);
            errorResponse.put("msg", "Database error: " + e.getMessage());
            return errorResponse;
        }
    }

    @PostMapping("/insertOrder")
    public Map<String,Object> insertOrder(@RequestBody Orders order){
        try {
            System.out.println("insertOrder:"+order.getCname()+order.getMurl()+order.getSeat()+order.getHall()+order.getPrice()+order.getMname()+order.getDay()+order.getTime()+order.getState());
            bookingService.insertOrder(order.getCname(),order.getMurl(),order.getSeat(),order.getHall(),order.getPrice(),order.getMname(),order.getDay(),order.getTime(),order.getState(),order.getNum()); // 调用Service层的方法
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("msg", "ok");
            return response;
        } catch (RuntimeException e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 600);
            errorResponse.put("msg", "Database error: " + e.getMessage());
            return errorResponse;
        }

    }
}
